package com.xiaotu.code.service.task;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xiaotu.code.model.quartz.QuartzCronExpression;
import com.xiaotu.code.service.quartz.QuartzCronExpressionService;

/**
 * 定时器重置(避免因修改定时时间配置而重启服务器).
 * @author chenxiangrong
 *
 */
public class QuartzCronExpressionResetTask {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	private boolean isRun = false;
	@Autowired
	private QuartzCronExpressionService quartzCronExpressionService;

	public void run() throws Exception {

		if (isRun) {
			return;
		}
		isRun = true;
		try {
			List<QuartzCronExpression> qces = quartzCronExpressionService
					.getAllQuartzCronExpression();

			if (CollectionUtils.isEmpty(qces)) {
				return;
			}
			for (QuartzCronExpression qce : qces) {
				try {
					quartzCronExpressionService.reSetScheduleJob(qce);
				} catch (Exception e) {
					logger.error("重新设置定时器时间配置错误，任务：" + qce.getBeanName(), e);
				}
			}
		} catch (Exception e) {
			logger.error("任务时间配置重置任务", e);
			throw e;
		} finally {
			isRun = false;
		}
	}
}
